********************************************************************************
*                                                                              *
*                         DATA SET-UP OF WOON DATA                             *
*                                                                              *
********************************************************************************

cd "U:\"

/// Data set-up 2002
use "woon 2002raw.dta", clear
g pc4 = pc02
g rent = huurtot if huurtot > 100
g price = verkwaar
g woz = onrzaakb
*g rentpoints = .
g size = opptbin
g sizealt = oppkeuke+opphfdwv
g type_appartment = typwon==6
g type_terraced = typwon==4 | typwon==5
g type_semidetached = typwon==2 | typwon==3
g type_detached = typwon==1
g floor = hfdwvert
replace floor = 0 if hfdwvert==.
g floorsbuilding = bouwlaag
replace floorsbuilding = 2 if bouwlaag<=0 | bouwlaag ==.
g elevator = lift==1
g garage = garcarp==1 | garcarp == 2
g centralheating = cv==1
g rooms = kamers
g maintgood = tonderho>3
g constryear = bjaar
g constrlt1945 = bjaar < 1945
g constr19451959 = bjaar >= 1945 & bjaar <= 1959
g constr19601970 = bjaar >= 1960 & bjaar <= 1970
g constr19711980 = bjaar >= 1971 & bjaar <= 1980
g constr19811990 = bjaar >= 1981 & bjaar <= 1990
g constr19912000 = bjaar >= 1991 & bjaar <= 2000 
g constrgt2000 = bjaar > 2000
g moveprop = verh==1
g movedw2yrs = verhuisd==1
g year = sysjaar

// Correct size
g sizerooms = size/rooms
centile sizerooms
replace sizerooms = r(c_1)
g sizecorr = rooms*sizerooms
g sizenew = size
replace sizenew = sizecorr if size < 30 | size > 1000
replace sizenew = sizecorr if size == 999
replace sizenew = sizealt if sizealt>1.25*size & sizealt !=.

// Calculate points
g ond1_2 = size
g ond3 = 2*max(centralheating*rooms, 1)
g ond4 = 36*(bjaar >= 2002) + 32*(bjaar>=2000 & bjaar<=2001) + 22*(bjaar>=1992 & bjaar<=1999) + 14*(bjaar>=1984 & bjaar<=1991) + 8*(bjaar>=1979 & bjaar<=1983) + 4*(bjaar>=1977 & bjaar<=1978)
centile oppkeuke, centile(15 50 85)
local small = r(c_1)
local med = r(c_2)
local large = r(c_3)
g ond5 = 4*(oppkeuke > `small' & oppkeuke < `large')+7*(oppkeuke >= `large')
g ond6 = 4+6*(baddouch==1)+4*(baddouch==2 | baddouch==4)+7*(baddouch==3)
g ond7 = (vhcap==1)*44
g ond8 = 2*(oppbui1>0 & oppbui1<25) + 4*(oppbui1>=25 & oppbui1<50) + 6*(oppbui1>=50 & oppbui1<75) +  8*(oppbui1>=75 & oppbui1<100) + 11*(oppbui1>=100)
g ond9 = int(woz/7995 + (woz/sizenew)/122)
replace ond9 = 20 if ond9 == .
g ond10 = 2*(nieuwvzn==1)
g ond14 = 25*(bjaar <= 1935)
g rentpoints = ond1_2 + ond3 + ond4 + ond5 + ond6 + ond7 + ond8 + ond9 + ond10 + ond14
replace rentpoints = . if rentpoints > 300 | rentpoints < 40

g socialrent = rent < 565.44 if rent !=. & year == 2002
replace socialrent = rent < 585.24 if rent !=. & year == 2003

keep respnr pc4 rent price woz rentpoints socialrent size sizealt type_* floor floorsbuilding elevator garage centralheating rooms maintgood constr* year move*
save "woon2002.dta", replace

use "woon 2006raw.dta", clear
g pc4 = postcode
g rent = khuurii if khuurii > 100
g price = verkwaar
g woz = woz
*g rentpoints = aantpunt if aantpunt < 500 & aantpunt > 50
g size = opptbin
g sizealt = opphfdwv+oppkeuke
g type_appartment = typwon==6
g type_terraced = typwon==4 | typwon==5
g type_semidetached = typwon==2 | typwon==3
g type_detached = typwon==1
g floor = hfdwvert
replace floor = 0 if hfdwvert==.
g floorsbuilding = bouwlaag
replace floorsbuilding = 2 if bouwlaag<=0 | bouwlaag ==.
g elevator = lift==1
g garage = garcarp==1 | garcarp == 2
g centralheating = vw2==1
g rooms = kamers
g maintgood = tonderho>3
g constryear = bjaar
g constrlt1945 = bjaar < 1945
g constr19451959 = bjaar >= 1945 & bjaar <= 1959
g constr19601970 = bjaar >= 1960 & bjaar <= 1970
g constr19711980 = bjaar >= 1971 & bjaar <= 1980
g constr19811990 = bjaar >= 1981 & bjaar <= 1990
g constr19912000 = bjaar >= 1991 & bjaar <= 2000 
g constrgt2000 = bjaar > 2000
g moveprop = verh==1
g movedw2yrs = verhuisd==1
g year = 2006 if sysmaand <=6
replace year = 2005 if sysmaand >=7

g socialrent = rent < 615.01 if rent !=. & year == 2006
replace socialrent = rent < 604.72 if rent !=. & year == 2005

// Correct size
g sizerooms = size/rooms
centile sizerooms
replace sizerooms = r(c_1)
g sizecorr = rooms*sizerooms
g sizenew = size
replace sizenew = sizecorr if size < 30 | size > 1000
replace sizenew = sizecorr if size == 999
replace sizenew = sizealt if sizealt>1.25*size & sizealt !=.

// Calculate points
g ond1_2 = size
g ond3 = 2*max(centralheating*rooms, 1)
g ond4 = 36*(bjaar >= 2002) + 32*(bjaar>=2000 & bjaar<=2001) + 22*(bjaar>=1992 & bjaar<=1999) + 14*(bjaar>=1984 & bjaar<=1991) + 8*(bjaar>=1979 & bjaar<=1983) + 4*(bjaar>=1977 & bjaar<=1978)
centile oppkeuke, centile(15 50 85)
local small = r(c_1)
local med = r(c_2)
local large = r(c_3)
g ond5 = 4*(oppkeuke > `small' & oppkeuke < `large')+7*(oppkeuke >= `large')
g ond6 = 4+6*(baddouch==1)+4*(baddouch==2 | baddouch==4)+7*(baddouch==3)
g ond7 = (vhcap==1)*44
g ond8 = 2*(oppbui1>0 & oppbui1<25) + 4*(oppbui1>=25 & oppbui1<50) + 6*(oppbui1>=50 & oppbui1<75) +  8*(oppbui1>=75 & oppbui1<100) + 11*(oppbui1>=100)
g ond9 = int(woz/7995 + (woz/sizenew)/122)
replace ond9 = 20 if ond9 == .
g ond10 = 2*(nieuwvzn==1)
g ond14 = 25*(bjaar <= 1935)
g rentpoints = ond1_2 + ond3 + ond4 + ond5 + ond6 + ond7 + ond8 + ond9 + ond10 + ond14
replace rentpoints = . if rentpoints > 300 | rentpoints < 40

keep respnr pc4 rent price woz rentpoints* socialrent size sizealt type_* floor floorsbuilding elevator garage centralheating rooms maintgood constr* year move*
save "woon2006.dta", replace


use "woon 2009raw.dta", clear
g pc4 = postcode
g rent = khuurii if khuurii > 100
g price = verkwaar
rename woz woz_
g woz = woz_*1000
*g rentpoints = aantpunt if aantpunt < 500 & aantpunt > 50
g size = opptbin
g sizealt = opphfdwv
g type_appartment = typwon==6
g type_terraced = typwon==4 | typwon==5
g type_semidetached = typwon==2 | typwon==3
g type_detached = typwon==1
g floor = hfdwvert
replace floor = 0 if hfdwvert==.
g floorsbuilding = bouwlaag
replace floorsbuilding = 2 if bouwlaag<=0 | bouwlaag ==.
g elevator = lift==1
g garage = garcarp==1 | garcarp == 2
g centralheating = vrwarm<=3
g rooms = kamers
g maintgood = tonderho>3
g constryear = bjaar
g constrlt1945 = bjaar < 1945
g constr19451959 = bjaar >= 1945 & bjaar <= 1959
g constr19601970 = bjaar >= 1960 & bjaar <= 1970
g constr19711980 = bjaar >= 1971 & bjaar <= 1980
g constr19811990 = bjaar >= 1981 & bjaar <= 1990
g constr19912000 = bjaar >= 1991 & bjaar <= 2000 
g moveprop = verh==1
g constrgt2000 = bjaar > 2000
g movedw2yrs = verhuisd==1
g year = 2009 if sysmaand <=6
replace year = 2008 if sysmaand >=7

g socialrent = rent < 647.53 if rent !=. & year == 2009
replace socialrent = rent < 631.73 if rent !=. & year == 2008

// Correct size
g sizerooms = size/rooms
centile sizerooms
replace sizerooms = r(c_1)
g sizecorr = rooms*sizerooms
g sizenew = size
replace sizenew = sizecorr if size < 30 | size > 1000
replace sizenew = sizecorr if size == 999
replace sizenew = sizealt if sizealt>1.25*size & sizealt !=.

// Calculate points
g ond1_2 = size
g ond3 = 2*max(centralheating*rooms, 1)
g ond4 = 36*(bjaar >= 2002) + 32*(bjaar>=2000 & bjaar<=2001) + 22*(bjaar>=1992 & bjaar<=1999) + 14*(bjaar>=1984 & bjaar<=1991) + 8*(bjaar>=1979 & bjaar<=1983) + 4*(bjaar>=1977 & bjaar<=1978)
g ond5 = 4*(keuken!=3)
g ond6 = 4+6*(baddouch==1)+4*(baddouch==2 | baddouch==4)+7*(baddouch==3)
g ond7 = (vhcap==1)*44
g ond8 = 2*(oppbui1>0 & oppbui1<25) + 4*(oppbui1>=25 & oppbui1<50) + 6*(oppbui1>=50 & oppbui1<75) +  8*(oppbui1>=75 & oppbui1<100) + 11*(oppbui1>=100)
g ond9 = int(woz/7995 + (woz/sizenew)/122)
replace ond9 = 20 if ond9 == .
g ond10 = 2*(nieuwvzn==1)
g ond14 = 25*(bjaar <= 1935)
g rentpoints = ond1_2 + ond3 + ond4 + ond5 + ond6 + ond7 + ond8 + ond9 + ond10 + ond14
replace rentpoints = . if rentpoints > 300 | rentpoints < 40

keep respnr pc4 rent price woz rentpoints socialrent size sizealt type_* floor floorsbuilding elevator garage centralheating rooms maintgood constr* year move*
save "woon2009.dta", replace

use "woon2012raw.dta", clear
destring respnr, force replace
order pc4, after(respnr)
g rent = khuurii if khuurii > 100
g price = Verkwaar
rename woz woz_
g woz = woz_
*g rentpoints = totaalpunten1 if totaalpunten1 < 500 & totaalpunten1>50
*replace rentpoints = huurpnt_imp if rentpoints==. & huurpnt_imp < 500
g size = OppTBin
g sizealt = Opphfdwv
g type_appartment = typwon==6
g type_terraced = typwon==4 | typwon==5
g type_semidetached = typwon==2 | typwon==3
g type_detached = typwon==1
g floor = Hfdwvert
replace floor = 0 if Hfdwvert==.
g floorsbuilding = Bouwlaag
replace floorsbuilding = 2 if Bouwlaag<=0 | Bouwlaag ==.
g elevator = Lift==1
g garage = GarCarp==1 | GarCarp == 2
g centralheating = VRWarm<=3
g rooms = Kamers
g maintgood = Tonderho>3
g constryear = BJaar
g constrlt1945 = BJaar < 1945
g constr19451959 = BJaar >= 1945 & BJaar <= 1959
g constr19601970 = BJaar >= 1960 & BJaar <= 1970
g constr19711980 = BJaar >= 1971 & BJaar <= 1980
g constr19811990 = BJaar >= 1981 & BJaar <= 1990
g constr19912000 = BJaar >= 1991 & BJaar <= 2000 
g constrgt2000 = BJaar > 2000
g moveprop = verh==1
g movedw2yrs = verhuisd==1
drop year
g year = 2012 if sysmaand <=6
replace year = 2011 if sysmaand >=7

g socialrent = rent < 664.66 if rent !=. & year == 2012
replace socialrent = rent < 652.52 if rent !=. & year == 2011

// Correct size
g sizerooms = size/rooms
centile sizerooms
replace sizerooms = r(c_1)
g sizecorr = rooms*sizerooms
g sizenew = size
replace sizenew = sizecorr if size < 30 | size > 1000
replace sizenew = sizecorr if size == 999
replace sizenew = sizealt if sizealt>1.25*size & sizealt !=.

// Calculate points
g ond1_2 = size
g ond3 = 2*max(centralheating*rooms, 1)
g ond4 = 36*(bjaar >= 2002) + 32*(bjaar>=2000 & bjaar<=2001) + 22*(bjaar>=1992 & bjaar<=1999) + 14*(bjaar>=1984 & bjaar<=1991) + 8*(bjaar>=1979 & bjaar<=1983) + 4*(bjaar>=1977 & bjaar<=1978)
g ond4_a = 44*(e_label==1) + 40*(e_label==2) + 36*(e_label==3) + 32*(e_label==4) + 22*(e_label==5) + 14*(e_label==6) + 8*(e_label==7) + 4*(e_label==8)
replace ond4 = ond4_a if ond4_a != .
g ond5 = 4*(Keuken!=3)
g ond6 = 4+6*(BadDouch==1)+4*(BadDouch==2 | BadDouch==4)+7*(BadDouch==3)
g ond7 = (Vhcap==1)*44
g oppbui1 = OppBui1+ OppBui2
g ond8 = 2*(oppbui1>0 & oppbui1<25) + 4*(oppbui1>=25 & oppbui1<50) + 6*(oppbui1>=50 & oppbui1<75) +  8*(oppbui1>=75 & oppbui1<100) + 11*(oppbui1>=100)
g ond9 = int(woz/7995 + (woz/sizenew)/122)
replace ond9 = 20 if ond9 == .
g ond10 = 1
g ond14 = 25*(bjaar <= 1935)
g rentpoints = ond1_2 + ond3 + ond4 + ond5 + ond6 + ond7 + ond8 + ond9 + ond10 + ond14
replace rentpoints = . if rentpoints > 300 | rentpoints < 40


keep respnr pc4 rent price woz rentpoints socialrent size sizealt type_* floor floorsbuilding elevator garage centralheating rooms maintgood constr* year move*
save "woon2012.dta", replace

use "woon2015raw.dta", clear
destring respnr, force replace
order pc4, after(respnr)
g rent = khuurii if khuurii > 100
g price = Verkwaar
rename woz woz_
g woz = woz_
*g rentpoints = huurpnt_imp if huurpnt_imp < 500 & huurpnt_imp > 50
g size = gebruiksopp
g sizealt = .
g type_appartment = WoonType==5
g type_terraced = WoonType==4 
g type_semidetached = WoonType==2 | WoonType==3
g type_detached = WoonType==1
g floor = VerdBegGr
replace floor = 0 if VerdBegGr==.
g floorsbuilding = Bouwlaag
replace floorsbuilding = 2 if Bouwlaag<=0 | Bouwlaag ==.
g elevator = Lift==1
g garage = GarCarp==1 | GarCarp == 2
g centralheating = VRWarm<=3
g rooms = Kamers
g maintgood = Tonderho>3
g constryear = bjaarbag
g constrlt1945 = bjaarbag < 1945
g constr19451959 = bjaarbag >= 1945 & bjaarbag <= 1959
g constr19601970 = bjaarbag >= 1960 & bjaarbag <= 1970
g constr19711980 = bjaarbag >= 1971 & bjaarbag <= 1980
g constr19811990 = bjaarbag >= 1981 & bjaarbag <= 1990
g constr19912000 = bjaarbag >= 1991 & bjaarbag <= 2000 
g constrgt2000 = bjaarbag > 2000
g moveprop = Verh==1
g movedw2yrs = Verhuisd==1
drop year
g year = 2015 if sysmaand <=6
replace year = 2014 if sysmaand >=7

g socialrent = rent < 710.68 if rent !=. & year == 2015
replace socialrent = rent < 699.48 if rent !=. & year == 2014

// Correct size
g sizerooms = size/rooms
centile sizerooms
replace sizerooms = r(c_1)
g sizecorr = rooms*sizerooms
g sizenew = size
replace sizenew = sizecorr if size < 30 | size > 1000
replace sizenew = sizecorr if size == 999
replace sizenew = sizealt if sizealt>1.25*size & sizealt !=.

// Calculate points
g ond1_2 = size
g ond3 = 2*max(centralheating*rooms, 1)
g ond4 = 36*(bjaarbag >= 2002) + 32*(bjaarbag>=2000 & bjaarbag<=2001) + 22*(bjaarbag>=1992 & bjaarbag<=1999) + 14*(bjaarbag>=1984 & bjaarbag<=1991) + 8*(bjaarbag>=1979 & bjaarbag<=1983) + 4*(bjaarbag>=1977 & bjaarbag<=1978)
g ond4_a = 36*(Energieklasse==1) + 32*(Energieklasse==2) + 22*(Energieklasse==3) + 14*(Energieklasse==4) + 8*(Energieklasse==5) + 4*(Energieklasse==6)
replace ond4 = ond4_a if ond4_a != .
g ond5 = 4*(Keuken!=3)
g ond6 = 4+6*(BadDouch==1)+4*(BadDouch==2 | BadDouch==4)+7*(BadDouch==3)
g ond7 = (VHCap==1)*44
g oppbui1 = OppBui1
g ond8 = 2*(oppbui1>0 & oppbui1<25) + 4*(oppbui1>=25 & oppbui1<50) + 6*(oppbui1>=50 & oppbui1<75) +  8*(oppbui1>=75 & oppbui1<100) + 11*(oppbui1>=100)
g ond9 = int(woz/7995 + (woz/sizenew)/122)
replace ond9 = 20 if ond9 == .
g ond10 = 1
g ond14 = 25*(bjaarbag <= 1935)
g rentpoints = ond1_2 + ond3 + ond4 + ond5 + ond6 + ond7 + ond8 + ond9 + ond10 + ond14
replace rentpoints = . if rentpoints > 300 | rentpoints < 40

keep respnr pc4 rent price woz rentpoints socialrent size sizealt type_* floor floorsbuilding elevator garage centralheating rooms maintgood constr* year move*
save "Data\woon2015.dta", replace

// Append data
clear all
append using "Data\woon2002.dta"
append using "Data\woon2006.dta"
append using "Data\woon2009.dta"
append using "Data\woon2012.dta"
append using "Data\woon2015.dta"
erase "Data\woon2002.dta"  
erase "Data\woon2006.dta" 
erase "Data\woon2009.dta"
erase "Data\woon2012.dta"
erase "Data\woon2015.dta"

merge m:1 pc4 using "kw_pc4.dta", nogenerate keep(3)
g inscorerule = (zscore > 7.29)
g scorerule = (zscore > 7.29) & year >= 2007
g inkw = kw
replace kw = 0 if year < 2007
g kw_1 = inkw*(year==2008 | year==2009)
g kw_2 = inkw*(year==2011 | year==2012)
g kw_3 = inkw*(year==2014 | year==2015)
g scorerule_1 = (zscore > 7.29) & (year==2008 | year==2009)
g scorerule_2 = (zscore > 7.29) & (year==2011 | year==2012)
g scorerule_3 = (zscore > 7.29) & (year==2014 | year==2015)
g kwexcl = (zscore > 7.29 & inkw == 0) | (zscore < 7.30 & inkw == 1)
merge m:1 pc4 year using "pc4data.dta", nogenerate keep(1 3)
merge m:1 pc4 year using "pc4_income.dta", nogenerate keep(1 3)

// Correct size
g sizerooms = size/rooms
centile sizerooms
replace sizerooms = r(c_1)
g sizecorr = rooms*sizerooms
*replace size = sizecorr if size < 30 | size > 1000
*replace size = sizecorr if size == 999
*replace size = sizealt if sizealt>1.25*size & sizealt !=.

// Correct woz/price values
global minrent 2.5
global maxrent 50

g rentsqm = rent/size
g rentsqmcorr = rent/sizecorr
g rentsqmalt = rent/sizealt
replace size = sizecorr if (rentsqm < $minrent | rentsqm > $maxrent) & rentsqm !=. & rentsqmcorr !=.
replace rentsqm = rentsqmcorr if (rentsqm < $minrent | rentsqm > $maxrent) & rentsqm !=. & rentsqmcorr !=.
replace size = sizealt if (rentsqm < $minrent | rentsqm > $maxrent) & rentsqm !=. & rentsqmalt !=.
replace rentsqm = rentsqmalt if (rentsqm < $minrent | rentsqm > $maxrent) & rentsqm !=. & rentsqmalt !=.
replace rentsqm = . if (rentsqm < $minrent | rentsqm > $maxrent) & rentsqm !=.
replace rent = . if rentsqm == .

g pricesqm = price/size
replace price = . if price < 25000 | price > 1500000
replace price = . if pricesqm < 250 | pricesqm > 5000 
replace pricesqm = price/size

g wozsqm = woz/size
replace woz = . if woz < 25000 | woz > 1500000
replace woz = . if wozsqm < 250 | wozsqm > 5000
replace wozsqm = woz/size

drop if size > 250 | size <25
drop if rooms > 25

// Create logged variables
g logrent = ln(rent)
g logrentsqm = ln(rent/size)
g logprice = ln(price)
g logpricesqm = ln(price/size)
g logwoz = ln(woz)
g logwozsqm = ln(woz/size)
g logrentpoints = ln(rentpoints)
g logsize = ln(size)
g logincome = ln(income)
g logpopdens = ln(popdens)
replace logpopdens = 0 if logpopdens == .
order logrent, after(rent)
order logrentpoints, after(rentpoints)
order logsize, after(size)
order logpopdens, after(popdens)
order logincome, after(income)
drop if woz==. & price==. & rent==.

// Create year variables
foreach i in 2002 2003 2005 2006 2008 2009 2011 2012 2014 2015 {
	g year_`i' = year==`i'
}

forvalues i = 1(1)3 {
	g zscore_`i' = (zscore-7.3)^`i'
	g zscorescrule_`i' = zscore_`i'*(zscore>=7.3)
	g zscorenscrule_`i' = zscore_`i'*(zscore<7.3)
	label variable zscore_`i' "z-score ^ `i'"
}



save "woon0215.dta", replace 

********************************************************************************
*                          GENERATE HOUSING STOCK                              *
********************************************************************************
clear all

foreach year in 2002 2006 2009 2012 2015 {

	use "woon `year'.dta", clear
	
	if `year' == 2002 {
		g pc4 = pc02
		drop if pc4 < 1000
		g year = sysjaar
		g ownocc = huureig==1
		drop if huurtot < 100 & ownocc!=1
		g socialrent = (huurtot<565.44) & (ownocc!=1) if year == 2002
		replace socialrent = (huurtot<585.24) & (ownocc!=1) if year == 2003
		g privrent = 1-socialrent-ownocc
		replace hweegwon = 0.1 if hweegwon < 0.1
		g wownocc = hweegwon*ownocc
		g wsocialrent = hweegwon*socialrent
		g wprivrent = hweegwon*privrent
		g weight = hweegwon
		collapse (sum) ownocc privrent socialrent wownocc wprivrent wsocialrent, by(pc4)
		g shownocc = ownocc/(ownocc+privrent+socialrent)
		g shprivrent = privrent/(ownocc+privrent+socialrent)
		g shsocialrent = socialrent/(ownocc+privrent+socialrent)
		g shwownocc = wownocc/(wownocc+wprivrent+wsocialrent)
		g shwprivrent = wprivrent/(wownocc+wprivrent+wsocialrent)
		g shwsocialrent = wsocialrent/(wownocc+wprivrent+wsocialrent)
		keep pc4 sh*
		g year = `year'
	}
	
	if `year' == 2006 {
		g pc4 = postcode
		drop if pc4 < 1000
		g year = 2006 if sysmaand <=6
		replace year = 2005 if sysmaand >=7
		g ownocc = aankprs > 0 & aankprs !=.
		drop if khuurii < 100 & ownocc!=1
		g socialrent = (khuurii<615.01) & (ownocc!=1) if year == 2006
		replace socialrent = (khuurii<604.72) & (ownocc!=1) if year == 2005
		g privrent = 1-socialrent-ownocc
		replace hweegwon = 0.1 if hweegwon < 0.1
		g wownocc = hweegwon*ownocc
		g wsocialrent = hweegwon*socialrent
		g wprivrent = hweegwon*privrent
		g weight = hweegwon
		collapse (sum) ownocc privrent socialrent wownocc wprivrent wsocialrent, by(pc4)
		g shownocc = ownocc/(ownocc+privrent+socialrent)
		g shprivrent = privrent/(ownocc+privrent+socialrent)
		g shsocialrent = socialrent/(ownocc+privrent+socialrent)
		g shwownocc = wownocc/(wownocc+wprivrent+wsocialrent)
		g shwprivrent = wprivrent/(wownocc+wprivrent+wsocialrent)
		g shwsocialrent = wsocialrent/(wownocc+wprivrent+wsocialrent)
		keep pc4 sh*
		g year = `year'
	}

	if `year' == 2009 {
		g pc4 = postcode
		drop if pc4 < 1000
		g year = 2009 if sysmaand <=6
		replace year = 2008 if sysmaand >=7
		g ownocc = aankprs > 0 & aankprs !=.
		drop if khuurii < 100 & ownocc!=1
		g socialrent = (khuurii<647.53) & (ownocc!=1) if year == 2009
		replace socialrent = (khuurii<631.73) & (ownocc!=1) if year == 2008
		g privrent = 1-socialrent-ownocc
		replace hweegwon = 0.1 if hweegwon < 0.1
		g wownocc = hweegwon*ownocc
		g wsocialrent = hweegwon*socialrent
		g wprivrent = hweegwon*privrent
		g weight = hweegwon
		collapse (sum) ownocc privrent socialrent wownocc wprivrent wsocialrent, by(pc4)
		g shownocc = ownocc/(ownocc+privrent+socialrent)
		g shprivrent = privrent/(ownocc+privrent+socialrent)
		g shsocialrent = socialrent/(ownocc+privrent+socialrent)
		g shwownocc = wownocc/(wownocc+wprivrent+wsocialrent)
		g shwprivrent = wprivrent/(wownocc+wprivrent+wsocialrent)
		g shwsocialrent = wsocialrent/(wownocc+wprivrent+wsocialrent)
		keep pc4 sh*
		g year = `year'
	}

	if `year' == 2012 {
		drop if pc4 < 1000
		replace year = 2012 if sysmaand <=6
		replace year = 2011 if sysmaand >=7
		g ownocc = AankPrs > 0 & AankPrs !=.
		drop if khuurii < 100 & ownocc!=1
		g socialrent = (khuurii<664.66) & (ownocc!=1) if year == 2012
		replace socialrent = (khuurii<652.52) & (ownocc!=1) if year == 2011
		g privrent = 1-socialrent-ownocc
		replace HWEEGWON = 0.1 if HWEEGWON < 0.1
		g wownocc = HWEEGWON*ownocc
		g wsocialrent = HWEEGWON*socialrent
		g wprivrent = HWEEGWON*privrent
		g weight = HWEEGWON
		collapse (sum) ownocc privrent socialrent wownocc wprivrent wsocialrent, by(pc4)
		g shownocc = ownocc/(ownocc+privrent+socialrent)
		g shprivrent = privrent/(ownocc+privrent+socialrent)
		g shsocialrent = socialrent/(ownocc+privrent+socialrent)
		g shwownocc = wownocc/(wownocc+wprivrent+wsocialrent)
		g shwprivrent = wprivrent/(wownocc+wprivrent+wsocialrent)
		g shwsocialrent = wsocialrent/(wownocc+wprivrent+wsocialrent)
		keep pc4 sh*
		g year = `year'
	}

	if `year' == 2015 {
		replace year = 2015 if sysmaand <=6
		replace year = 2014 if sysmaand >=7
		g ownocc = AankPrs > 0 & AankPrs !=.
		drop if khuurii < 100 & ownocc!=1
		g socialrent = (khuurii<710.68) & (ownocc!=1) if year == 2015
		replace socialrent = (khuurii<699.48) & (ownocc!=1) if year == 2014
		g privrent = 1-socialrent-ownocc
		replace hweegwon = 0.1 if hweegwon < 0.1
		g wownocc = hweegwon*ownocc
		g wsocialrent = hweegwon*socialrent
		g wprivrent = hweegwon*privrent
		g weight = hweegwon
		collapse (sum) ownocc privrent socialrent wownocc wprivrent wsocialrent, by(pc4)
		g shownocc = ownocc/(ownocc+privrent+socialrent)
		g shprivrent = privrent/(ownocc+privrent+socialrent)
		g shsocialrent = socialrent/(ownocc+privrent+socialrent)
		g shwownocc = wownocc/(wownocc+wprivrent+wsocialrent)
		g shwprivrent = wprivrent/(wownocc+wprivrent+wsocialrent)
		g shwsocialrent = wsocialrent/(wownocc+wprivrent+wsocialrent)
		keep pc4 sh*
		g year = `year'
	}

	label variable shownocc "share owner-occupied housing"
	label variable shprivrent "share private rental housing"
	label variable shsocialrent "share social rental housing"
	label variable shwownocc "weighted share owner-occupied housing"
	label variable shwprivrent "weighted share private rental housing"
	label variable shwsocialrent "weighted share social rental housing"
	label variable year "year of observation"
	label variable pc4 "postcode 4-digit"
	order pc4 year, first
	
	save "pc4_houingunits_`year'.dta", replace
}

clear all

foreach year in 2002 2006 2009 2012 2015 {
	append using "pc4_houingunits_`year'.dta"
	erase "pc4_houingunits_`year'.dta"
}

replace shwownocc = shownocc if shwownocc == .
replace shwprivrent = shprivrent if shwprivrent == .
replace shwsocialrent = shsocialrent if shwsocialrent == .
drop if pc4 == .

merge 1:1 pc4 year using "pc4_housingunits_cbs.dta", nogen keep(1 3)
g shownocc_cbs = ownerocc/(ownerocc+publhousing+rentalhousing)
g shprivrent_cbs = rentalhousing/(ownerocc+publhousing+rentalhousing)
g shsocialrent_cbs = publhousing/(ownerocc+publhousing+rentalhousing)
label variable shownocc_cbs "share owner-occupied housing based on housing stock information"
label variable shprivrent_cbs "share private rental housing on housing stock information"
label variable shsocialrent_cbs "share social rental housing on housing stock information"
drop ownerocc publhousing rentalhousing unknown totalunits

save "pc4_housingunits.dta", replace
	